//
//  HHVisitorView.swift
//  HHWeibo
//
//  Created by hehuafeng on 2017/2/18.
//  Copyright © 2017年 雷厉峰行. All rights reserved.
//

import UIKit

class HHVisitorView: UIView {
    
    // 注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    // 登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton("登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    
    // 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    // MARK: - 私有属性
    // 信息字典
    var visitorInfo: [String : String]? {
        didSet {
            // 1. 取出图片名、tipLabel显示文字
            guard let imageName = visitorInfo?["imageName"], let message = visitorInfo?["message"] else {
                return
            }
            
            // 2. 设置显示信息
            tipLabel.text = message
            
            // 3. 设置图像, "首页" 不需要设置
            if imageName == "" {
                // 只有首页才需要开启“轮转动画”
                setupAnimation()
                
                return
            }
            iconView.image = UIImage(named: imageName)
            
            // 4. 其它控制器隐藏小房子、遮盖物
            houseView.isHidden = true
            maskIconView.isHidden = true
        }
    }
    
    
    // 图像视图
    lazy var iconView = UIImageView(image:
        UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮罩视图
    lazy var maskIconView = UIImageView(image:
        UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 小房子
    lazy var houseView = UIImageView(image:
        UIImage(named: "visitordiscover_feed_image_house"))
    
    // 提示标签
    lazy var tipLabel: UILabel = UILabel.cz_label(withText: "关注一些人, 回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    
}

// MARK: - 界面设置
extension HHVisitorView {
    
    func setupAnimation() {
        // 1. 创建属性动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        // 2. 设置动画属性
        anim.toValue = 2 * M_PI      // 动画旋转角度
        anim.duration = 15           // 动画重复时间
        anim.repeatCount = MAXFLOAT  // 动画重复次数
        
        // 3. 设置动画执行完毕不移除
        anim.isRemovedOnCompletion = false
        
        // 4. 添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    /// 初始化界面
    func setupUI() {
        // 1. 设置背景颜色
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        
        // 2. 添加子控件
        addSubview(iconView)
        addSubview(maskIconView)  // 添加位置非常重要
        addSubview(houseView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 3. tipLabel居中显示
        tipLabel.textAlignment = .center
        
        // MARK: 自动布局
        // 禁用AutoresizingMask
        for view in subviews {
            view.translatesAutoresizingMaskIntoConstraints = false
        }
        
        // 设置约束
        // 1. iconView
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: self,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        
        addConstraint(NSLayoutConstraint.init(item: iconView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: self,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: -60))
        
        // 2. houseView
        addConstraint(NSLayoutConstraint.init(item: houseView,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        
        addConstraint(NSLayoutConstraint.init(item: houseView,
                                              attribute: .centerY,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerY,
                                              multiplier: 1.0,
                                              constant: 0))
        
        // 3. tipLabel
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .centerX,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .centerX,
                                              multiplier: 1.0,
                                              constant: 0))
        
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 236))
        
        addConstraint(NSLayoutConstraint.init(item: tipLabel,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: iconView,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 20))
        
        // 4. registerButton
        addConstraint(NSLayoutConstraint.init(item: registerButton,
                                              attribute: .left,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .left,
                                              multiplier: 1.0,
                                              constant: 0))
        
        addConstraint(NSLayoutConstraint.init(item: registerButton,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 10))
        
        addConstraint(NSLayoutConstraint.init(item: registerButton,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 80))
        
        // 5. loginButton
        addConstraint(NSLayoutConstraint.init(item: loginButton,
                                              attribute: .right,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .right,
                                              multiplier: 1.0,
                                              constant: -6))
        
        addConstraint(NSLayoutConstraint.init(item: loginButton,
                                              attribute: .top,
                                              relatedBy: .equal,
                                              toItem: tipLabel,
                                              attribute: .bottom,
                                              multiplier: 1.0,
                                              constant: 10))
        
        addConstraint(NSLayoutConstraint.init(item: loginButton,
                                              attribute: .width,
                                              relatedBy: .equal,
                                              toItem: nil,
                                              attribute: .notAnAttribute,
                                              multiplier: 1.0,
                                              constant: 80))
        
        // 6. maskIconView
        let views: [String : Any] = ["maskIconView" : maskIconView,
                                     "registerButton" : registerButton]
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: views))
        
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(-20)-[registerButton]",
            options: [],
            metrics: nil,
            views: views))
    }
}
